package com.sc.nft.admin.rpc;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.BoxTakePoolDao;
import com.sc.nft.dao.UserCreatorInfoDao;
import com.sc.nft.entity.req.ConvertNftSearchRpcRequest;
import com.sc.nft.entity.vo.ActivityPropsVO;
import com.sc.nft.entity.vo.BoxPoolVO;
import com.sc.nft.entity.vo.BoxTakeConversionConditionCombination;
import com.sc.nft.entity.vo.GetDaoByIdVO;
import com.sc.nft.service.ActivityPropsService;
import com.sc.nft.service.BoxTakeConversionConditionService;
import com.sc.nft.sup.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author 刘国航
 * @since 2024/8/21 16:58
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/rpc/convert")
@Api(tags = "兑换接口")
public class ConvertRpc {

    private final BoxTakePoolDao boxTakePoolDao;
    private final BoxTakeConversionConditionService boxTakeConversionConditionService;
    private final ActivityPropsService activityPropsService;
    private final UserCreatorInfoDao userCreatorInfoDao;

    @ApiOperation(value = "NFT兑换列表", notes = "NFT兑换列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/nftPage", produces = MediaType.APPLICATION_JSON_VALUE)
    public PageResult<BoxPoolVO> nftPage(@RequestBody ConvertNftSearchRpcRequest request) {
        Page<BoxPoolVO> boxPoolVOPage = boxTakePoolDao.voucherConversion(true, request.getBoxName(), request.getConvertScenario(), request.getDaoId(), request.getPageNo(), request.getPageSize());
        boxPoolVOPage.getRecords().forEach(boxPoolVO -> {
            BoxTakeConversionConditionCombination boxTakeConversionConditionCombination = boxTakeConversionConditionService.boxTakeConversionConditionCombination(boxPoolVO.getId());
            boxPoolVO.setConditionName(boxTakeConversionConditionCombination.getConditionName());
            boxPoolVO.setCollectionName(boxTakeConversionConditionCombination.getCollectionName());
            if (Objects.nonNull(boxPoolVO.getDaoId())) {
                GetDaoByIdVO getDaoByIdVO = userCreatorInfoDao.getDaoById(boxPoolVO.getDaoId());
                if (Objects.nonNull(getDaoByIdVO)) {
                    boxPoolVO.setDaoName(getDaoByIdVO.getDaoName());
                }
            }
        });
        return PageResult.success(boxPoolVOPage);
    }

    @ApiOperation(value = "盲盒兑换列表", notes = "盲盒兑换列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/boxPoolPage", produces = MediaType.APPLICATION_JSON_VALUE)
    public PageResult<BoxPoolVO> boxPoolPage(@RequestBody ConvertNftSearchRpcRequest request) {
        Page<BoxPoolVO> boxPoolVOPage = boxTakePoolDao.boxPoolPage(true, request.getBoxName(), request.getConvertScenario(), request.getDaoId(), request.getPageNo(), request.getPageSize());
        boxPoolVOPage.getRecords().forEach(boxPoolVO -> {
            BoxTakeConversionConditionCombination boxTakeConversionConditionCombination = boxTakeConversionConditionService.boxTakeConversionConditionCombination(boxPoolVO.getId());
            boxPoolVO.setConditionName(boxTakeConversionConditionCombination.getConditionName());
            boxPoolVO.setCollectionName(boxTakeConversionConditionCombination.getCollectionName());
            if (Objects.nonNull(boxPoolVO.getDaoId())) {
                GetDaoByIdVO getDaoByIdVO = userCreatorInfoDao.getDaoById(boxPoolVO.getDaoId());
                if (Objects.nonNull(getDaoByIdVO)) {
                    boxPoolVO.setDaoName(getDaoByIdVO.getDaoName());
                }
            }
        });
        return PageResult.success(boxPoolVOPage);
    }

    @ApiOperation(value = "权益道具兑换列表", notes = "权益道具兑换列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/activityPropsPage", produces = MediaType.APPLICATION_JSON_VALUE)
    public PageResult<ActivityPropsVO> activityPropsPage(@RequestBody ConvertNftSearchRpcRequest request) {
        return PageResult.success(activityPropsService.adminPage(true, request.getBoxName(), request.getConvertScenario(), request.getDaoId(), request.getPageNo(), request.getPageSize()));
    }

}
